<?php


namespace App\Bx;


use App\Elastic\EsModel;

class BxElastic extends EsModel
{
    const INDEX = 'bx';

    public static function getMapping(): array
    {
        return include(__DIR__ . '/mapping.php');
    }

    public static function getSetting(): array
    {
        return [
            'number_of_shards' => 3,
            'number_of_replicas' => 1
        ];
    }

    public static function getIndex(): string
    {
        return sprintf('%s%s%s', 'demo_', self::INDEX, time());
    }

    public static function getAlias(): string
    {
        return self::INDEX;
    }

    public static function rebuild()
    {
        // TODO: Implement rebuild() method.
    }

    public function pushToIndex($datas)
    {
        $params = [];
        $update_at = date('Y-m-d H:i:s');
        foreach ($datas as $data) {
            $params['body'][] = [
                'index' => [
                    '_index' => static::getAlias(),
                    '_type' => '_doc',
                    '_id' => $data['id']
                ]
            ];
            $data['updated_at'] = $update_at;
            $params['body'][] = $data;
        }
        return $this->client->bulk($params);
    }
}